/*---------------------------------------------------------------------------*/
/*   Sample code for unit test , using google test          
/*   File Name    :  getst.cpp
/*   Complier     :  C++ 11
/*   Author       :  Shibo Jiang     
/*   Instructions :  Initial file.             2020/5/27              V0.1
/*          
/*---------------------------------------------------------------------------*/

/* Include Files */
#include <vector>
#include "gtest/gtest.h"

extern "C" {
    #include "sample_func.h"
}

/* System Function  ---------------------------------------------------------*/
int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

static bool ArrayCompare(const int* exp, const int* act, int len) 
{
    bool flag = true;
    for (int i = 0; i < len; i++)
    {
        if (exp[i] != act[i])
        {
            flag = false;
            break;
        }
    }
    return flag; 
}
/* Function end--------------------------------------------------------------*/

/* Test Suit IntSwapTest-----------------------------------------------------*/
TEST(IntSwapTest, swapSuccess)
{
    int a = 1;
    int b = 2; 
    int_swap(&a, &b); 
    EXPECT_EQ(2, a) << "Expect a is 2";
    EXPECT_EQ(1, b);
}

TEST(IntSwapTest, paramError)
{
    int a = 1; 
    int_swap(&a, NULL); 
    EXPECT_EQ(1, a);
    int_swap(NULL, &a); 
    EXPECT_EQ(1, a);
}
/* Function end--------------------------------------------------------------*/

/* Test Suit SqrtTest--------------------------------------------------------*/
TEST(SqrtTest, normalTest) 
{
    EXPECT_NEAR(1e-50, sqrt(1e-100), 1e-12);
    EXPECT_NEAR(1, sqrt(1), 1e-12);
    EXPECT_NEAR(1e50, sqrt(1e100), 1e-12);
}
/* Function end--------------------------------------------------------------*/

/* Test Suit ByteSwapTest----------------------------------------------------*/
TEST(ByteSwapTest, stringSwap)
{
    char str1[10] = "123456789";
    char str2[10] = "abcdefghi"; 
    byte_swap(str1, str2, 5); 
    EXPECT_STREQ("abcde6789", str1) << "Expect str1 is abcde6789"; 
    EXPECT_STREQ("12345fghi", str2) << "Expect str2 is 12345fghi"; 
}

TEST(ByteSwapTest, paramError)
{
    char str1[10] = "123456789";
    char str2[10] = "abcdefghi"; 
    byte_swap(str1, NULL, 5);
    byte_swap(NULL, str2, 5);
    byte_swap(str1, str2, 0); 
    EXPECT_STREQ("123456789", str1);
    EXPECT_STREQ("abcdefghi", str2);
}
/* Function end--------------------------------------------------------------*/

/* Test Suit BubblSortTest---------------------------------------------------*/
TEST(BubblSortTest, normalTest) 
{
    int expArray[5] = { 1, 2, 3, 4, 5 };
    int actArray[5] = { 3, 1, 5, 4, 2 };
    bubblesort(actArray, 5);
    EXPECT_PRED3(ArrayCompare, expArray, actArray, 5);
}
/* Function end--------------------------------------------------------------*/


